/*
 * jxUtils Project (2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.text.MaskFormatter;

import org.jxUtils.check.CheckUtil;
import org.jxUtils.check.ParameterCheck;
import org.jxUtils.exception.InvalidValueException;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxUtils
 * @package org.jxUtils.date
 * @date 28/05/2013 - 19:57:50
 */
public final class TimeFormatter {
	
	private MaskFormatter maskFormatter;
	
	private String pattern;
	
	/**
	 * pattern = dd/MM/yyyy<br>
	 * mask = ##/##/####
	 */
	public TimeFormatter() {
	
		this("dd/MM/yyyy", "##/##/####");
	}
	
	/**
	 * @param pattern
	 * @param mask
	 */
	public TimeFormatter(final String pattern, final String mask) {
	
		this(pattern, mask, '_');
	}
	
	/**
	 * @param pattern
	 * @param mask
	 * @param placeholder
	 */
	public TimeFormatter(final String pattern, final String mask, final char placeholder) {
	
		setFormatter(pattern, mask, placeholder);
	}
	
	/**
	 * @return the maskFormatter or null
	 */
	public final MaskFormatter getMaskFormatter() {
	
		return maskFormatter;
	}
	
	/**
	 * @return the pattern
	 */
	public final String getPattern() {
	
		return pattern;
	}
	
	/**
	 * @return the mask or null
	 */
	public final String getMask() {
	
		return maskFormatter != null ? maskFormatter.getMask() : null;
	}
	
	/**
	 * @param pattern
	 * @param mask
	 */
	public final void setFormatter(final String pattern, final String mask) {
	
		setFormatter(pattern, mask, '_');
	}
	
	/**
	 * @param pattern
	 * @param mask
	 * @param placeholder
	 */
	public final void setFormatter(final String pattern, final String mask, final char placeholder) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(pattern, "pattern");
		
		try {
			// 
			new SimpleDateFormat(pattern).format(new Date());
			
			//
			if(CheckUtil.isNotNullAndNotAbsoluteEmpty(mask)) {
				
				maskFormatter = new MaskFormatter(mask);
				maskFormatter.setPlaceholderCharacter(placeholder);
				
			}else {
				
				maskFormatter = null;
			}
			
			this.pattern = pattern;
			
		}catch(final IllegalArgumentException illegalArgumentException) {
			
			throw new InvalidValueException(illegalArgumentException);
			
		}catch(final ParseException parseException) {
			
			throw new InvalidValueException(parseException);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
	
		final int prime = 31;
		int result = 1;
		result = prime * result + (maskFormatter == null ? 0 : maskFormatter.hashCode());
		result = prime * result + (pattern == null ? 0 : pattern.hashCode());
		return result;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
	
		if(this == obj) {
			return true;
		}
		if(obj == null) {
			return false;
		}
		if( !(obj instanceof TimeFormatter)) {
			return false;
		}
		final TimeFormatter other = (TimeFormatter)obj;
		if(maskFormatter == null) {
			if(other.maskFormatter != null) {
				return false;
			}
		}else if( !maskFormatter.equals(other.maskFormatter)) {
			return false;
		}
		if(pattern == null) {
			if(other.pattern != null) {
				return false;
			}
		}else if( !pattern.equals(other.pattern)) {
			return false;
		}
		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
	
		return "[pattern=" + (pattern != null ? pattern : "") + ", mask=" + (maskFormatter != null ? maskFormatter.getMask() : "") + "]";
	}
}
